<template>
  <div class="app-container">
    <div v-loading="listLoading" :element-loading-text="loadingText" class="content">
      <dnd-list :target-list="targetList" :source-list="sourceList" target-list-title="目标列表" source-list-title="源列表" />
    </div>

    <el-row v-show="isShow">
      <el-button type="primary" @click="dndListHandle()">保存设置</el-button>
    </el-row>
  </div>
</template>

<script>

import DndList from '@/components/DndList'
import { getDndList, dndListHandle } from '@/api/menu'

export default {
  name: 'QuickNavEdit',
  components: { DndList },
  data() {
    return {
      targetList: [],
      sourceList: [],
      listLoading: false,
      loadingText: '',
      isShow: false
    }
  },
  created() {
    this.getDndList()
  },
  methods: {
    async getDndList() {
      this.listLoading = true
      this.loadingText = '正在加载中...'
      const res = await getDndList()
      this.listLoading = false
      this.isShow = true
      this.sourceList = res.data.sourceList
      this.targetList = res.data.targetList
    },
    dndListHandle() {
      this.isShow = false
      this.listLoading = true
      this.loadingText = '正在保存中...'
      const params = {
        target: this.targetList
      }
      dndListHandle(params).then(response => {
        this.listLoading = false
        this.isShow = true
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.$nextTick(() =>
          this.$router.go(0)
        )
      })
    }
  }
}
</script>
